﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DTTB.Common;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.DanhMuc;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.DanhMuc;
using Webdiyer.WebControls.Mvc;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.DanhMucDelegate
{
    public class TienLuongCoBanDelegate
    {
        DBDanhMucDataContext DBDanhMuc = new DBDanhMucDataContext();

        public IList<dm_TienLuongCoBan> GetAllTienLuongCoBan()
        {
            return DBDanhMuc.dm_TienLuongCoBans.Where(p => p.IsDelete == false).ToList();
        }
        public dm_TienLuongCoBan AddNewTienLuongCoBan(dm_TienLuongCoBan TienLuongCoBan)
        {
            DBDanhMuc.dm_TienLuongCoBans.InsertOnSubmit(TienLuongCoBan);

            DBDanhMuc.SubmitChanges();

            return GetLoaiCongChucIdMax();
        }

        public void DeleteTienLuongCoBan(int TienLuongCoBanID)
        {
            dm_TienLuongCoBan TienLuongCoBanNew =
                DBDanhMuc.dm_TienLuongCoBans.Single(p => p.Id == TienLuongCoBanID);
            //LoaiCongChuc.Id = LoaiCongChuc.Id;

            DBDanhMuc.dm_TienLuongCoBans.DeleteOnSubmit(TienLuongCoBanNew);
            DBDanhMuc.SubmitChanges();
        }
        public int DeleteTienLuongCoBan(dm_TienLuongCoBan TienLuongCoBan)
        {
            dm_TienLuongCoBan TienLuongCoBanDelete =
                DBDanhMuc.dm_TienLuongCoBans.Single(p => p.Id == TienLuongCoBan.Id);
            if (TienLuongCoBanDelete.hs_NhanVien_LuongCoBans.Count() == 0)
            {
                TienLuongCoBanDelete.Id = TienLuongCoBan.Id;
                TienLuongCoBanDelete.UpdateDate = DateTime.Now;
                TienLuongCoBanDelete.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
                TienLuongCoBanDelete.IsDelete = true;
                DBDanhMuc.SubmitChanges();
                return 1;
            }
            else
            {
                return 0;
            }
        }
        public void UpdateTienLuongCoBan(dm_TienLuongCoBan TienLuongCoBan)
        {
            dm_TienLuongCoBan TienLuongCoBanUpdate =
                DBDanhMuc.dm_TienLuongCoBans.Single(p => p.Id == TienLuongCoBan.Id);

            TienLuongCoBanUpdate.Id = TienLuongCoBan.Id;
            TienLuongCoBanUpdate.TienLuongCoBan = TienLuongCoBan.TienLuongCoBan;
            TienLuongCoBanUpdate.UpdateDate = TienLuongCoBanUpdate.UpdateDate;
            TienLuongCoBanUpdate.UpdateBy = TienLuongCoBanUpdate.UpdateBy;
            DBDanhMuc.SubmitChanges();
        }

        public PagedList<dm_TienLuongCoBan> SearchTienLuongCoBanPagedList(int? id, string searchCriteria)// id = page Index
        {
            return (PagedList<dm_TienLuongCoBan>)DBDanhMuc.dm_TienLuongCoBans.Where(p => p.TienLuongCoBan == decimal.Parse(searchCriteria) && p.IsDelete == false).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);


        }

        public dm_TienLuongCoBan GetTienLuongCoBanById(string id)
        {
            try
            {
                dm_TienLuongCoBan item = this.DBDanhMuc.dm_TienLuongCoBans.Single(p => p.Id == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public dm_TienLuongCoBan GetTienLuongCoBanByTenCheck(decimal ten)
        {

            IEnumerable<dm_TienLuongCoBan> list = this.DBDanhMuc.dm_TienLuongCoBans.Where(p => p.TienLuongCoBan == ten && p.IsDelete == false);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public dm_TienLuongCoBan GetTienLuongCoBanByTen(decimal ten)
        {

            IEnumerable<dm_TienLuongCoBan> list = this.DBDanhMuc.dm_TienLuongCoBans.Where(p => p.TienLuongCoBan == ten);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }
        public PagedList<dm_TienLuongCoBan> RecycleGetTienLuongCoBanPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_TienLuongCoBans.Where(p => p.IsDelete == true).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }
        public PagedList<dm_TienLuongCoBan> GetTienLuongCoBanPagedList(int? id)// id = page Index
        {
            return DBDanhMuc.dm_TienLuongCoBans.Where(p => p.IsDelete == false).OrderByDescending(p => p.Id).ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }

        public dm_TienLuongCoBan BuildNewTienLuongCoBan(TienLuongCoBanForm form)
        {
            dm_TienLuongCoBan newTienLuongCoBan = new dm_TienLuongCoBan();
            if (!string.IsNullOrEmpty(form.TienLuongCB))
                newTienLuongCoBan.TienLuongCoBan = decimal.Parse(form.TienLuongCB.Replace(".", ""), null);
            newTienLuongCoBan.CreateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            newTienLuongCoBan.CreateDate = DateTime.Now;
            newTienLuongCoBan.IsDelete = false;
            return newTienLuongCoBan;
        }
        public int PhucHoiTienLuongCoBan(int id)
        {
            dm_TienLuongCoBan phuchoi = new dm_TienLuongCoBan();
            phuchoi = this.GetTienLuongCoBanById(Convert.ToString(id));
            dm_TienLuongCoBan item = new dm_TienLuongCoBan();
            item = this.GetTienLuongCoBanByTenCheck(phuchoi.TienLuongCoBan.Value);
            if (item != null)
                return 1;
            else
            {
                phuchoi.IsDelete = false;
                DBDanhMuc.SubmitChanges();
                return 2;
            }
        }
        public dm_TienLuongCoBan BuildEditTienLuongCoBan(TienLuongCoBanForm form)
        {
            dm_TienLuongCoBan newTienLuongCoBan = new dm_TienLuongCoBan();
            newTienLuongCoBan.Id = form.id;
            if (!string.IsNullOrEmpty(form.TienLuongCB))
                newTienLuongCoBan.TienLuongCoBan = decimal.Parse(form.TienLuongCB.Replace(".", ""), null);
            newTienLuongCoBan.UpdateDate = DateTime.Now;
            newTienLuongCoBan.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            return newTienLuongCoBan;
        }
        public dm_TienLuongCoBan GetLoaiCongChucIdMax()
        {
            IList<dm_TienLuongCoBan> list = DBDanhMuc.dm_TienLuongCoBans.ToList();
            return list.Count != 0 ? list.ElementAt(list.Count - 1) : null;
        }
        public TienLuongCoBanForm ConvertToForm(dm_TienLuongCoBan obj)
        {
            TienLuongCoBanForm form = new TienLuongCoBanForm();
            form.id = obj.Id;
            form.TienLuongCB = obj.TienLuongCoBan != null ? obj.TienLuongCoBan.Value.ToString("#,#").Replace(",", ".") : "";
            form.UpdateDate = obj.UpdateDate != null ? obj.UpdateDate.Value : DateTime.MinValue;
            form.CreateDate = obj.CreateDate != null ? obj.CreateDate.Value : DateTime.MinValue;
            form.UpdateBy = obj.UpdateBy != null ? obj.UpdateBy.Value : 0;
            form.CreateBy = obj.CreateBy != null ? obj.CreateBy.Value : 0;
            return form;
        }
    }
}